﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chowpoo.Common;
using Chowpoo.Util;
using CourseSystem.Common;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;
using CourseSystem.Stat.ActionFilter;
using Webdiyer.WebControls.Mvc;

namespace CourseSystem.Web.Controllers
{
    [UserAuth(UserType = UserTypeEnum.Teacher)]
    public class AdminResourcesController : BaseController
    {

        #region 过期方法
        /*
        public ActionResult Text()
        {
            return View();
        }

        public ActionResult TextListUc(int? pid, byte? type)
        {
            byte ty = type.CheckByte(0);
            CommonRequest.ClearCache();
            var list = TextResourcesBLL.GetList(ty).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult TextEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = TextResourcesBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return View(info);
            }
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult TextEdit(TextResourcesInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return View();
            if (info.Id != 0)
            {
                var oldInfo = TextResourcesBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                oldInfo.Author = info.Author;
                oldInfo.Detail = info.Detail;
                oldInfo.Title = info.Title;
                oldInfo.Type = info.Type;
                if (TextResourcesBLL.Edit(oldInfo))
                {
                    SetAlertMsg("编辑成功.");
                    return View(oldInfo);
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            info.Hits = 0;
            info.ReviewType = 1;
            info.UploadDate = DateTime.Now;
            info.UploaderId = UserHelper.LoginUser.Id;
            info.UploaderType = 1;
            if (TextResourcesBLL.Add(info))
            {
                SetAlertMsg("编辑成功.");
                return View();
            }
            SetAlertMsg("未知错误.");
            return View();
        }

        public ActionResult TextDel(int id)
        {
            if (TextResourcesBLL.Del(id))
                SetAlertMsg("删除成功.");
            return RedirectToAction("Text");
        }

        public ActionResult Img()
        {
            return View();
        }

        public ActionResult ImgListUc(int? pid, byte? type)
        {
            byte ty = type.CheckByte(0);
            CommonRequest.ClearCache();
            var list = ImgResourcesBLL.GetList(ty).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView(model);
        }

        public ActionResult ImgEdit(int? id)
        {
            if (id.HasValue)
            {
                var info = ImgResourcesBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return View(info);
            }
            return View();
        }

        [HttpPost]
        public ActionResult ImgEdit(ImgResourcesInfo info)
        {
            ViewData.Model = info;
            if (!ModelState.IsValid)
                return View();
            var msg = "";
            var fileName = ImgHelper.UploadImg(Request.Files["UpImgFile"], out msg);
            if (!string.IsNullOrEmpty(msg))
            {
                SetAlertMsg(msg);
                return View();
            }
            if (info.Id != 0)
            {
                var oldInfo = ImgResourcesBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();

                oldInfo.FileName = fileName;
                oldInfo.Title = info.Title;
                oldInfo.Type = info.Type;
                if (ImgResourcesBLL.Edit(oldInfo))
                {
                    SetAlertMsg("编辑成功.");
                    return View(oldInfo);
                }
                SetAlertMsg("未知错误.");
                return View();
            }
            info.UploaderId = UserHelper.LoginUser.Id;
            info.UploaderType = (byte)UserHelper.LoginUser.UserType;
            info.UploadDate = DateTime.Now;
            info.Hits = 0;
            info.FileName = fileName;
            if (ImgResourcesBLL.Add(info))
            {
                SetAlertMsg("添加成功.");
                return View();
            }
            SetAlertMsg("未知错误.");
            return View();
        }
        */
        #endregion

        public ActionResult CommonViewUc(int id)
        {
            var info = CommonResourcesBLL.Get(id);
            if (info == null)
                return NotFound();
            return View(info);
        }

        /// <summary>
        /// 1文本2图像3Flash4音频5视频6PPT7学习包
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [UrlStatFilter]
        public ActionResult Common(byte type)
        {
            return View(ResourcesHelper.GetTypeByEn(type));
        }

        public ActionResult CommonListUc(int? pid, string title, byte type, byte? minType, int? isReview)
        {
            isReview = isReview.CheckInt(-1);
            byte ty = minType.CheckByte(0);
            CommonRequest.ClearCache();
            var list = CommonResourcesBLL.GetList(type, ty, title, "", isReview.Value).OrderByDescending(o => o.Id);
            var model = list.ToPagedList(pid ?? 1, CommonHelper.PageSize);
            return PartialView("CommonListUc", model);
            // return PartialView(ResourcesHelper.GetTypeByEn(type) + "ListUc", model);
        }

        public ActionResult CommonEdit(byte? type, int? id)
        {
            if (id.HasValue)
            {
                var info = CommonResourcesBLL.Get(id.Value);
                if (info == null)
                    return NotFound();
                return View(ResourcesHelper.GetTypeByEn(info.Type) + "Edit", info);
            }
            if (!type.HasValue)
                return NotFound();
            var reView = ResourcesHelper.GetTypeByEn(type.Value) + "Edit";

            return View(reView, new CommonResourcesInfo { Type = type.Value });
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CommonEdit(CommonResourcesInfo info)
        {
            if (info.Id != 0)
            {
                var oldInfo = CommonResourcesBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                info.FileName = oldInfo.FileName;
            }
            var viewRe = View(ResourcesHelper.GetTypeByEn(info.Type) + "Edit");
            ViewData.Model = info;
            if (string.IsNullOrEmpty(info.Author) && string.IsNullOrEmpty(info.Source))
                ModelState.AddModelError("Author", "需填写作者或来源");

            if (!ModelState.IsValid)
                return viewRe;
            info.FileName = info.FileName ?? "";
            info.Author = info.Author ?? "";
            info.Message = info.Message ?? "";
            info.Title = info.Title ?? "";
            info.Source = info.Source ?? "";
            info.Keyword = info.Keyword ?? "";
            info.Introduction = info.Introduction ?? "";
            info.CoverImg = info.CoverImg ?? "";
            /* if (info.Type != 1)
             {*/
            var msg = "";
            var fileName = ResourcesHelper.UploadFile(Request.Files["UpFile"], info.Type, out msg, info.Id == 0);
            if (!string.IsNullOrEmpty(msg))
            {
                SetAlertMsg(msg);
                return viewRe;
            }
            info.FileName = fileName;
            //视频
            if (info.Type == 5)
            {
                fileName = ResourcesHelper.UploadFile(Request.Files["UpFileCover"], 2, out msg, false);
                if (!string.IsNullOrEmpty(msg))
                {
                    SetAlertMsg(msg);
                    return viewRe;
                }
                if (!string.IsNullOrEmpty(fileName))
                    info.CoverImg = fileName;
            }

            if (info.Id != 0)
            {
                var oldInfo = CommonResourcesBLL.Get(info.Id);
                if (oldInfo == null)
                    return NotFound();
                var needDelFile = "";
                if (!string.IsNullOrEmpty(info.FileName))
                {
                    needDelFile = oldInfo.FileName;
                    oldInfo.FileName = info.FileName;
                }
                oldInfo.IsCanDownload = info.IsCanDownload;
                oldInfo.Title = info.Title;
                oldInfo.MinType = info.MinType;
                oldInfo.Keyword = info.Keyword;
                oldInfo.Introduction = info.Introduction;
                oldInfo.Source = info.Source;
                oldInfo.Author = info.Author;
                oldInfo.Message = info.Message;
                var needDelFile2 = "";
                if (!string.IsNullOrEmpty(info.CoverImg))
                {
                    needDelFile2 = oldInfo.CoverImg;
                    oldInfo.CoverImg = info.CoverImg;
                }
                if (CommonResourcesBLL.Edit(oldInfo))
                {
                    if (!string.IsNullOrEmpty(needDelFile))
                        System.IO.File.Delete(Server.MapPath("~/") + needDelFile);
                    if (!string.IsNullOrEmpty(needDelFile2))
                        System.IO.File.Delete(Server.MapPath("~/") + needDelFile2);
                    SetAlertMsg("编辑成功.");
                    ViewData.Model = oldInfo;
                    return viewRe;
                }
                SetAlertMsg("未知错误.");
                return viewRe;
            }
            info.UploaderId = UserHelper.LoginUser.Id;
            info.UploaderType = (byte)UserHelper.LoginUser.UserType;
            info.UploadDate = DateTime.Now;
            info.Hits = 0;
            info.IsReview = true;
            if (CommonResourcesBLL.Add(info))
            {
                SetAlertMsg("添加成功.");
                return viewRe;
            }
            SetAlertMsg("未知错误.");
            return viewRe;
        }


        public ActionResult CommonDel(int id)
        {
            var info = CommonResourcesBLL.Get(id);
            if (info == null)
                return NotFound();
            if (CommonResourcesBLL.Del(id))
            {
                ResourcesHelper.DeleteFile(info.FileName);
                SetAlertMsg("删除成功.", true);
            }
            return RedirectToAction("Common", new { type = info.Type });
        }

        public ActionResult ResourcePass(int id)
        {
            var info = CommonResourcesBLL.Get(id);
            if (info == null)
                return NotFound();
            info.IsReview = true;
            if (CommonResourcesBLL.Edit(info))
            {
                SetAlertMsg("设置成功.", true);
            }
            else
                SetAlertMsg("设置失败.", true);
            return RedirectToAction("Common", new { type = info.Type });

        }

    }
}
